package com.sunlong.jiangwei.service.impl;

import com.sunlong.jiangwei.dao.AsideMenuMapper;
import com.sunlong.jiangwei.entity.AsideMenu;
import com.sunlong.jiangwei.service.IAsideMenu;
import com.sunlong.jiangwei.vo.AsideMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Service
public class AsideMenuImpl implements IAsideMenu {
    @Autowired
    private AsideMenuMapper asideMenuMapper;
    @Override
    public List<AsideMenu> selectMenuItemsAll() {
        Integer integer;
        List<AsideMenu> byParent = getAsideMenusByParent(0);
        for (int i = 0; i < byParent.size(); i++) {
            AsideMenu asideMenu = byParent.get(i);
            List<AsideMenu> byParent1 = getAsideMenusByParent(asideMenu.getId());
            asideMenu.setChildren(byParent1);
        }
        return byParent;
    }

    @Override
    public List<AsideMenuVo> list() {
        List<AsideMenu> asideMenus = this.selectMenuItemsAll();
        ArrayList<AsideMenuVo> menuVos = new ArrayList<>();
        for (AsideMenu menu : asideMenus) {
            AsideMenuVo asideMenuVo = new AsideMenuVo();
            asideMenuVo.setValue(menu.getId());
            asideMenuVo.setLabel(menu.getAuthName());
            ArrayList<AsideMenuVo> children = new ArrayList<>();
            for (AsideMenu child : menu.getChildren()) {
                AsideMenuVo asideMenuVo1 = new AsideMenuVo();
                asideMenuVo1.setValue(child.getId());
                asideMenuVo1.setLabel(child.getAuthName());
                children.add(asideMenuVo1);
            }
            asideMenuVo.setChildren(children);
            menuVos.add(asideMenuVo);
        }
        return menuVos;
    }

    private List<AsideMenu> getAsideMenusByParent(int parent) {
        Example example = new Example(AsideMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parent",parent);
        return asideMenuMapper.selectByExample(example);
    }
}
